﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using phudieuhungthinh.BL;
using MichaelKappel.Net.CSharp;

namespace phudieuhungthinh
{
    public partial class LayoutDetails : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string MenuKey = "LeftMenu";

            if (!IsPostBack)
            {
                string menuHtml = HelperCache.Get<string>(MenuKey);
                if (menuHtml == null)
                    lock (this)
                    {
                        if (menuHtml == null)
                        {
                            menuHtml = BuildMenu();
                            HelperCache.Add(MenuKey, menuHtml, HelperCache.CachingTime.ShortTermSliding);
                        }
                    }

                pnlMenuContainer.Controls.Add(new LiteralControl(menuHtml));
            }
        }

        private string BuildMenu()
        {
            StringBuilder menuBuilder = new StringBuilder("<div class='end'>");
            CategoryImpl im = new CategoryImpl();
            var menus = im.GetCategoryMenus();

            menuBuilder.Append("<ul>");
            foreach (var item in menus)
            {
                menuBuilder.AppendFormat("<li><a href='/chuyen-muc/{0}/{1}'>{2}</a>", item.Url, string.Empty, item.Name);
                if (item.SubMenus.Count > 0)
                {
                    menuBuilder.Append("<ul>");
                    foreach (var subItem in item.SubMenus)
                    {
                        menuBuilder.AppendFormat("<li><a href='/chuyen-muc/{0}/{1}'>{2}</a></li>", subItem.Url, string.Empty, subItem.Name);
                    }
                    menuBuilder.Append("</ul>");
                }
                menuBuilder.Append("</li>");
            }
            menuBuilder.Append("</ul>");
            menuBuilder.Append("</div>");

            return menuBuilder.ToString();
        }
    }
}